package com.wingsfrog.puzzle.util.wechat;

import com.google.gson.Gson;
import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class WeiXinUtil {
    public class ParseCode {
        public String access_token;
        public String expires_in;
        public String refresh_token;
        public String openid;
        public String scope;
    }
    public class UserInfo {
        public String openid;
        public String nickname;
        public Byte sex;
        public String province;
        public String city;
        public String country;
        public String headimgurl;
        public String language;
        public String[] privilege;
        public String unionid;
    }
    private final static Logger logger = Logger.getLogger(WeiXinUtil.class);
    private final static Gson gson = new Gson();

    public static String getWeixinToken() throws IOException {
        URL url = new URL("http://wz.wingsfrog.com/access_token.php");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setDoInput(true);
        connection.setUseCaches(false);
        connection.setDoOutput(true);
        connection.connect();

        DataOutputStream out = new DataOutputStream(connection.getOutputStream());
        String content = "{\"uniacid\": 9}";
        out.writeBytes(content);
        out.flush();
        out.close(); // flush and close

        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line = reader.readLine();
        reader.close();
        logger.info("access_token:"+line);
        return line;
    }

    public static ParseCode getUserTokenByCode(String code) {
        try {
            String _url = WechatConst.createSnsCodeUrl(code);
            URL url = new URL(_url);
            HttpURLConnection connection = null;
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.connect();
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line = reader.readLine();
            return gson.fromJson(line, ParseCode.class);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static UserInfo getUserInfo(String accessToekn, String openId) {
        try {
            String _url = WechatConst.createSnsUserInfoUrl(accessToekn,openId);
            URL url = new URL(_url);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.connect();
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()
                    , "UTF-8"));
            String line = reader.readLine();
            logger.info(line);
            return gson.fromJson(line, UserInfo.class);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
